package com.easypan.utils;

import com.easypan.exception.BusinessException;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author VectorX
 * @version 1.0.0
 * @description
 * @date 2024/07/28
 */
public class StringUtils
{

    public static void checkParam(Object param) {
        try {
            Field[] fields = param
                    .getClass()
                    .getDeclaredFields();
            boolean notEmpty = false;
            for (Field field : fields) {
                String methodName = "get" + StringUtils.upperCaseFirstLetter(field.getName());
                Method method = param
                        .getClass()
                        .getMethod(methodName);
                Object object = method.invoke(param);
                if (object != null && object instanceof String && !StringUtils.isEmpty(object.toString()) || object != null && !(object instanceof String)) {
                    notEmpty = true;
                    break;
                }
            }
            if (!notEmpty) {
                throw new BusinessException("多参数更新，删除，必须有非空条件");
            }
        }
        catch (BusinessException e) {
            throw e;
        }
        catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("校验参数是否为空失败");
        }
    }

    public static String upperCaseFirstLetter(String field) {
        if (isEmpty(field)) {
            return field;
        }
        //如果第二个字母是大写，第一个字母不大写
        if (field.length() > 1 && Character.isUpperCase(field.charAt(1))) {
            return field;
        }
        return field
                .substring(0, 1)
                .toUpperCase() + field.substring(1);
    }

    public static boolean isEmpty(String str) {
        if (null == str || "".equals(str) || "null".equals(str) || "\u0000".equals(str)) {
            return true;
        }
        else if ("".equals(str.trim())) {
            return true;
        }
        return false;
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }
}
